var express = require('express');
var router = express.Router();
const admin = require("../sql/admin");
const sql = require("../sql/sql");

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('login');
});

router.post('/in', function(req, res, next) {
  // res.send(req.body);
  let obj = req.body;
  sql.find(admin, obj).then(data=>{
    if(data.length > 0){
      // res.send({
      //   tit:"登录成功",
      //   msg:obj
      // })

      // 登录成功之后，存储登录状态
      // 1.通过cookie存储状态
      // res.cookie("isLogin","ok");
      
      // 2.通过session存储状态
      req.session.isLogin = "ok";
      req.session.power = data[0].power;

      res.redirect("/");
    }else{
      res.redirect("/login");
    }
  })
});

module.exports = router;
